import { getAreaListByCityApi, getCityListByProvinceApi, getStreetListByAreaApi } from "@/api/common";
import { addDriverApi } from "@/api/managementCenter/driverManagement";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { ref } from "vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { formDataRef, formState, streetOpts, emit, passData } = state;
  const { resetFields } = FormHook({ formState });
  const selectAreaObj = ref<Base.GeneraDataType>({});
  const submitForm = ref<Array<Base.GeneraDataType>>([]);
  const onFinish = () => {
    const { streetCode } = formState;
    // const { name } = streetOpts.value.find((t: any) => t.code === streetCode) || {};

    streetCode.forEach((e: any) => {
      const { name } = streetOpts.value.find((t: any) => t.code === e) || {};
      submitForm.value.push({
        ...selectAreaObj.value,
        streetCode: e,
        streetName: name
      });
    });

    // submitForm.value.push({
    //   ...selectAreaObj.value,
    //   streetCode,
    //   streetName: name
    // });
    selectAreaObj.value = {};
    resetFields();
  };
  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const keepHandle = () => {
    addDriverApi({
      userId: passData.value.userId,
      bizUserDriverAreaReqDtos: submitForm.value
    }).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!");
        emit("submit");
      }
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    submitForm.value = [];
    emit("update:visible", false);
  };
  const loadData = (selectedOptions: any) => {
    const targetOption = selectedOptions[selectedOptions.length - 1];
    let requestFn: any;
    switch (targetOption.type) {
      case 1:
        requestFn = getCityListByProvinceApi;
        break;
      case 2:
        requestFn = getAreaListByCityApi;
        break;
      case 3:
        requestFn = getStreetListByAreaApi;
        break;
    }
    if (Object.prototype.toString.call(requestFn) === "[object Function]") {
      targetOption.loading = true;
      requestFn(targetOption.value)
        .then((res: any) => {
          const { code, data } = res;
          if (code === 200) {
            targetOption.children = data.map((t: any) => {
              return {
                value: t.code,
                label: t.name,
                type: t.type,
                isLeaf: t.type === 4
              };
            });
          }
        })
        .finally(() => (targetOption.loading = false));
    }
  };
  const changeAreaHandle = (value: Array<string>, selectedOptions: Array<Base.GeneraDataType>) => {
    if (!value || !value.length) {
      streetOpts.value = [];
      selectAreaObj.value = {};
      formState.streetCode = undefined;
    } else {
      selectAreaObj.value = {
        provinceName: selectedOptions[0].name,
        provinceCode: selectedOptions[0].code,
        cityName: selectedOptions[1].name,
        cityCode: selectedOptions[1].code,
        areaCode: selectedOptions[2].code,
        areaName: selectedOptions[2].name
      };
      formState.streetCode = undefined;
      getStreetListByAreaApi(value[2]).then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          streetOpts.value = data || [];
        }
      });
    }
  };
  const deleteHandle = (index: number) => {
    submitForm.value.splice(index, 1);
  };

  const filterOption = (input: string, option: any) => {
    console.log({ input, option });
    return String(option.label).indexOf(String(input)) >= 0;
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields,
    loadData,
    changeAreaHandle,
    submitForm,
    selectAreaObj,
    deleteHandle,
    keepHandle,
    filterOption
  };
}
